package com.example.wzdjcrdpassword.repository;

import com.example.wzdjcrdpassword.entity.CardPassword;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface CardPasswordRepository extends JpaRepository<CardPassword, Long> {
    
    @Query("SELECT c FROM CardPassword c WHERE c.cardPassword = :cardPassword AND c.isDeleted = 'NO' AND c.startTime <= :currentTime AND c.expireTime >= :currentTime")
    Optional<CardPassword> findByCardPasswordAndValid(@Param("cardPassword") String cardPassword, 
                                                      @Param("currentTime") LocalDateTime currentTime);
    
    Optional<CardPassword> findByBusinessIdAndIsDeleted(String businessId, String isDeleted);
    
    @Query("SELECT c FROM CardPassword c WHERE c.isDeleted = 'NO' AND c.startTime <= :currentTime AND c.expireTime >= :currentTime")
    List<CardPassword> findAllAvailableCardPasswords(@Param("currentTime") LocalDateTime currentTime);
}